package com.migo.api;

import com.migo.annotation.IgnoreAuth;
import com.migo.entity.MgOpenAdvertisementEntity;
import com.migo.entity.MgSampleVideoEntity;
import com.migo.service.MgSampleVideoService;
import com.migo.utils.PageUtils;
import com.migo.utils.Query;
import com.migo.utils.R;
import com.migo.utils.redis.RedisCacheUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * PVMG10样片
 *
 * @author powervision
 * @email zhenrong.xu@powervision.me
 * @date 2021-03-29 19:35:46
 */
@CrossOrigin
@RestController
@RequestMapping("/api/mgsamplevideo")
public class ApiMgSampleVideoController {
    @Autowired
    private MgSampleVideoService mgSampleVideoService;

    @Autowired
    private RedisCacheUtil<List<MgSampleVideoEntity>> redisCacheUtil;

    /**
     * 列表
     */
    @IgnoreAuth
    @RequestMapping("/mgSampleVideolist")
    public R mgSampleVideolist(@RequestParam Map<String, Object> params) {
        //默认值防止出错
        if (params.containsKey("language")) {
            params.put("language", "en");
        }
        List<MgSampleVideoEntity> mgSampleVideoList = null;
        String key = "mgSampleVideolist_" + params.get("language");
        if (redisCacheUtil.hashKey(key)) {
            mgSampleVideoList = redisCacheUtil.getCacheList(key);
            if (params.containsKey("flush")) {  //刷新标志
                redisCacheUtil.deleteObject(key);
            }
        } else {
            //查询数据库写入redis
            synchronized (this) {
                mgSampleVideoList = mgSampleVideoService.querySampleVideoList(params);
                redisCacheUtil.setCacheList(key, mgSampleVideoList);
            }
        }
        //查询列表数据
//        List<MgSampleVideoEntity> mgSampleVideoList = mgSampleVideoService.querySampleVideoList(params);
        return R.ok().put("data", mgSampleVideoList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        MgSampleVideoEntity mgSampleVideo = mgSampleVideoService.queryObject(id);

        return R.ok().put("mgSampleVideo", mgSampleVideo);
    }


}
